package com.sam4cloud.system.project.controller;

import com.sam4cloud.common.core.lock.Lock;
import com.sam4cloud.common.core.model.R;
import com.sam4cloud.system.project.cons.CacheCons;
import com.sam4cloud.system.project.model.dto.SysUserDTO;
import com.sam4cloud.system.project.model.entity.SysUser;
import com.sam4cloud.system.project.model.vo.SysUserVO;
import com.sam4cloud.system.project.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2022-11-09
 */
@Api(tags = "系统用户表")
@RestController
@RequestMapping("/sysUser")
public class SysUserRestController {

    @Autowired
    private ISysUserService sysUserService;


    @Lock(key = "#id", waitTime = 20, leaseTime = 10)
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "单条查询")
    @Cacheable(value = CacheCons.USER_DETAILS, key = "#id")
    public R getUser(@PathVariable("id") String id) {
        SysUser user = sysUserService.getById(id);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return R.ok(user);
    }


    @PostMapping("/edit")
    @ApiOperation(value = "单条修改")
    @CacheEvict(value = CacheCons.USER_DETAILS, key = "#sysUserDTO.id")
    public R editUser(@RequestBody SysUserDTO sysUserDTO) {
        sysUserService.updateById(sysUserDTO.convert(SysUser.class));
        return R.ok();
    }
}

